//
//  EmoticonManager.swift
//  14-表情键盘
//
//  Created by 高伦展 on 2018/2/19.
//  Copyright © 2018年 glz. All rights reserved.
//

import Foundation

class EmoticonManager {
    
    //单例
    static let shareManager = EmoticonManager()
    
    //表情包模型
    lazy var packages = [EmoticonPackage]()
    //MARK: - 构造函数
    private init() {
        
        packages.append(EmoticonPackage(dict: ["group_name_cn":"最近" as AnyObject]))
        
        //1.加载 emoticon.plist - 如果文件不存在。path == nil
        guard let path = Bundle.main.path(forResource: "emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle") else{
            return
        }
        //2.加载‘字典’
        guard let dict = NSDictionary(contentsOfFile: path) else{
            return
        }
        //3.从字典中获得 id 的数组
        let array = (dict["packages"] as! NSArray).value(forKey: "id") as! [String]
        //4.遍历 id 数组，准备加载 info.plist
        for id in array{
            loadInfoPlist(id: id)
        }
    }
    //加载每一个 id 目录下的 info.plist
    private func loadInfoPlist(id:String){
        //1.声称目录
        let path = Bundle.main.path(forResource: "info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        //2.加载‘字典’
        let dict = NSDictionary(contentsOfFile: path) as! [String:AnyObject]
        
        packages.append(EmoticonPackage(dict:dict))
    }
}
